package ims.db.update.product;

import ims.beans.Product;
import ims.db.AbstractDAO;
import ims.db.AbstractDbRequest;
import ims.db.AbstractDbResponse;

import java.sql.SQLException;
import java.sql.Statement;

public class UpdateProductDAO extends AbstractDAO {

	public UpdateProductDAO() throws ClassNotFoundException, SQLException {
		super();
	}

	@Override
	public AbstractDbResponse execute(AbstractDbRequest dbRequest) 
	{
		UpdateProductDbResponse response = new UpdateProductDbResponse();
		UpdateProductDbRequest request = (UpdateProductDbRequest)dbRequest;
        try 
        {
        	Statement stmt = conn.createStatement();
        	Product product = request.getProduct();
        	StringBuffer sql = new StringBuffer();
        	sql.append("update Product set prodDesc ='").append(product.getProdDesc()).append("',");
        	sql.append("prodTypeId='").append(product.getProdTypeId()).append("',");
        	sql.append("price='").append(product.getPrice());
        	sql.append("' where pid ='").append(product.getPid()).append("'");
        	
    		int retCd = stmt.executeUpdate(sql.toString());
    		if (retCd == 1)
    			response.setMsg("Product : "+ product.getProdDesc()+" updated successfully");
    		else
    			response.setMsg("Unable update product: "+ product.getProdDesc());
    		
            stmt.close();
            conn.close();
        } 
        catch (SQLException sqlEx)
        {
        	sqlEx.printStackTrace();
        }
        return response;
	}
}
